package main

import (
	"fmt"
	"github.com/360EntSecGroup-Skylar/excelize"
	"github.com/pkgng/structor"
	"strconv"
	"time"
)

type Info struct {
	structor.BaseStructor `structor:"alist"`
	Hrid                  string `structor:"alist[0]"`
	StuId                 string `structor:"alist[1]"`
	Name                  string `structor:"alist[2]"`
	Phone                 string `structor:"alist[3]"`
	QQ                    string `structor:"alist[4]"`
	Job                   string `structor:"alist[5]"`
	Work                  string `structor:"alist[6]"`
	Status                string `structor:"alist[7]"`
	Time                  string `structor:"alist[8]"`
	IsDeleted             string `structor:"alist[9]"`
}

func ContainsInfo(infos []Info, info Info) bool {
	if len(infos) == 0 {
		return false
	}
	for _, item := range infos {
		if info.Name == item.Name && info.Phone == item.Phone && info.QQ == item.QQ {
			return true
		}
	}
	return false
}

func emain() {
	const sheet = "hr3"

	f, _ := excelize.OpenFile("./hr4.xlsx")

	rows := f.GetRows(sheet)

	var roleMap = map[int]string{
		3: "程序",
		4: "设计",
		5: "内容",
		6: "运营",
		7: "行政",
		8: "产品策划",
	}

	var infos []Info

	formatTimeStr := "2021-09-01 00:00:00"

	formatTime, _ := time.Parse("2006-01-02 15:04:05", formatTimeStr)

	timeLimit := formatTime.Unix()

	for index, item := range rows {
		var info Info
		err := structor.New().Set("alist", item).Construct(&info)
		if err != nil {
			return
		}
		if index != 0 {
			if ContainsInfo(infos, info) {
				continue
			}
			if info.IsDeleted == "1" {
				continue
			}
			tmp, _ := strconv.Atoi(info.Time)
			if int64(tmp) < timeLimit {
				continue
			}
			info.Time = time.Unix(
				int64(tmp),
				0,
			).Format("2006-01-02 15:04:05")
			tmp, _ = strconv.Atoi(info.Job)
			info.Job = roleMap[tmp]
		}
		infos = append(infos, info)
	}

	fmt.Println(infos)

	e := excelize.NewFile()
	indexSheet := f.NewSheet("sheet1")
	index := 1
	for _, item := range infos {
		strIndex := strconv.Itoa(index)
		e.SetCellValue("Sheet1", "A"+strIndex, item.Hrid)
		e.SetCellValue("Sheet1", "B"+strIndex, item.StuId)
		e.SetCellValue("Sheet1", "C"+strIndex, item.Name)
		e.SetCellValue("Sheet1", "D"+strIndex, item.Phone)
		e.SetCellValue("Sheet1", "E"+strIndex, item.QQ)
		e.SetCellValue("Sheet1", "F"+strIndex, item.Job)
		e.SetCellValue("Sheet1", "G"+strIndex, item.Work)
		e.SetCellValue("Sheet1", "H"+strIndex, item.Status)
		e.SetCellValue("Sheet1", "I"+strIndex, item.Time)
		e.SetCellValue("Sheet1", "J"+strIndex, item.IsDeleted)
		index++
	}

	e.SetActiveSheet(indexSheet)

	if err := e.SaveAs("./名单.xlsx"); err != nil {
		fmt.Println(err)
	}
}
